package com.xhwl.data.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.dto.cent.sdata.ProjectDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.query.cent.sdata.AttrQuery;
import com.xhwl.data.service.IProjectExtraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Class
 * @Description 项目拓展信息
 * @Auther zhouxingxiang
 * @Date 2023/7/4 14:00
 */
@RestController
public class ProjectExtraController {

    @Autowired
    IProjectExtraService projectExtraService;

    /**
     * 查询企业下的所有项目拓展字段
     *
     * @param attrQuery
     * @return
     */
    @RequestMapping(value = "/project-extra/get-extra-attr", method = RequestMethod.POST)
    public ResultJson getExtraAttr(AttrQuery attrQuery) {
        if (null == attrQuery.getEnterpriseId()){
            return ResultJson.fail("参数错误，企业id为空");
        }
        return ResultJson.success(projectExtraService.getExtraAttr(attrQuery));
    }

    /**
     * 查询项目下的已保存的拓展字段信息
     *
     * @param projectId
     * @return
     */
    @RequestMapping(value = "/project-extra/get", method = RequestMethod.POST)
    public ResultJson get(Integer projectId) {
        if (null == projectId){
            return ResultJson.fail("参数错误，项目id为空");
        }
        return ResultJson.success(JSON.parseObject(projectExtraService.getExtraInfo(projectId)));
    }

    /**
     * 保存项目的拓展信息
     *
     * @param projectDTO
     * @return
     */
    @RequestMapping(value = "/project-extra/save", method = RequestMethod.POST)
    public ResultJson save(@RequestBody ProjectDTO projectDTO) {
        projectExtraService.save(JSONObject.toJSONString(projectDTO.getExtraInfo()), projectDTO.getId());
        return ResultJson.success();
    }

}
